var macro;
var jsLF = "\n";
var i, retcode, errtext;
var MyDelay;
var MyPosts;

var Aldeias;

var espioes;
var cavalariaLeve;
var command;
var aldeiaAtacante = "43215";

i = 0;

espioes = 1;
cavalariaLeve = 15;
//command = commandPega();



Aldeias = new Array();

Aldeias.push("44086");
Aldeias.push("46711");
Aldeias.push("47608");
Aldeias.push("43817");
Aldeias.push("44703");
Aldeias.push("45180");
Aldeias.push("45929");
Aldeias.push("46833");
Aldeias.push("42370");
Aldeias.push("46870");
Aldeias.push("43509");
Aldeias.push("42024");
Aldeias.push("44054");
Aldeias.push("44347");
//Aldeias.push("44714");
//Aldeias.push("47190");
//Aldeias.push("82518");
//Aldeias.push("42955");
//Aldeias.push("43376");
//Aldeias.push("45523");
//Aldeias.push("46685");
//Aldeias.push("43809");
//Aldeias.push("46656");
//Aldeias.push("43374");
//Aldeias.push("44536");
//Aldeias.push("44814");
//Aldeias.push("44527");
//Aldeias.push("48311");
//Aldeias.push("43636");
//Aldeias.push("45603");
//Aldeias.push("46377");
//Aldeias.push("43008");
//Aldeias.push("44399");
//Aldeias.push("44434");





//
//a1 = "32667";
//if(!conteins(a1)){
//   Aldeias.push(a1);
//}






var log =  "";

MyDelay = 0;

for (i ; i < Aldeias.length; i++) {
    
    log = "Executando "+i+ " de " + (Aldeias.length -1) +jsLF + log;
    
    iimDisplay(log);
    43215
    iimSet("POSTID", Aldeias[i]);
    
    macro = "CODE:";
    macro += "URL GOTO=http://br36.tribalwars.com.br/game.php?village="+aldeiaAtacante+"&screen=place&target={{postid}}" + jsLF;
    macro += "TAG POS=1 TYPE=TD ATTR=ID:inner-border EXTRACT=HTM";
    iimPlay(macro);

    var text = iimGetLastExtract();
     
    var numCavalariaLeve = text.substr(text.indexOf("('#unit_input_light')")+23,10);
    numCavalariaLeve = numCavalariaLeve.substr(0,numCavalariaLeve.indexOf(")"));
    numCavalariaLeve = parseInt(numCavalariaLeve);
    //  alert(numCavalariaLeve);

    if(numCavalariaLeve<cavalariaLeve){
        //alert("não ha cavalos suficientes. Necessário "+cavalariaLeve);
        macro = "CODE:";
        macro += "WAIT SECONDS=60" + jsLF;
        iimPlay(macro);
        i--
        continue;
    }

    var numSpy = text.substr(text.indexOf("('#unit_input_spy')")+20,10);
    numSpy = numSpy.substr(0,numSpy.indexOf(")"));
    numSpy = parseInt(numSpy);
    //    alert(numSpy);

    if(numSpy<espioes){
        //alert("não ha espioes. Necessário "+espioes);
        macro = "CODE:";
        macro += "WAIT SECONDS=60" + jsLF;
        iimPlay(macro);
        i--
        continue;

    }


    macro = "CODE:";
    macro += "TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:units ATTR=ID:unit_input_spy CONTENT="+ espioes + jsLF;
    macro += "TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:units ATTR=ID:unit_input_light CONTENT=" + cavalariaLeve + jsLF;
    macro += "TAG POS=1 TYPE=INPUT:SUBMIT FORM=ID:units_form ATTR=ID:target_attack" + jsLF;
    macro += "TAG POS=1 TYPE=TD ATTR=ID:inner-border EXTRACT=HTM";
    
    iimPlay(macro);

    text = iimGetLastExtract();
    command = text.substr(text.indexOf("action=command&amp;h=")+21,4);
    
    macro = "CODE:";
    macro += "TAG POS=1 TYPE=INPUT:SUBMIT FORM=ACTION:http://br36.tribalwars.com.br/game.php?village="+aldeiaAtacante+"&screen=place&action=command&h="+ command +" ATTR=ID:troop_confirm_go" + jsLF;
    macro += "WAIT SECONDS=" + MyDelay + jsLF;
    

    retcode = iimPlay(macro);
    if (retcode < 0) {              // an error has occured
        errtext = iimGetLastError();
        alert(errtext);
        i--;

    //commandPega();
        
    // break;
    }
}







function commandPega(){
    var macro = "CODE:";
    macro += "URL GOTO=http://br36.tribalwars.com.br/game.php?village="+aldeiaAtacante+"&screen=place&target=32667" + jsLF;
    macro += "TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:units ATTR=ID:unit_input_spy CONTENT="+ espioes + jsLF;
    macro += "TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:units ATTR=ID:unit_input_light CONTENT=" + cavalariaLeve + jsLF;
    macro += "TAG POS=1 TYPE=INPUT:SUBMIT FORM=ID:units_form ATTR=ID:target_attack" + jsLF;
    //macro += "TAG POS=1 TYPE=BODY ATTR=CLASS:* EXTRACT=HTM";
    macro += "TAG POS=1 TYPE=TD ATTR=ID:inner-border EXTRACT=HTM";
	
    var retcode = iimPlay(macro);

    //retcode = iimPlay(macro);
    	
    var text = iimGetLastExtract();
    text = text.substr(text.indexOf("action=command&amp;h=")+21,4);
    alert(text);
    
    return text;
}

function conteins(aldeia){
    var i;
    for(i = 0; i<Aldeias.length;i++){
        if(Aldeias[i]== aldeia)
            return true;
    }
    return false;
}
